package com.jeesite.modules.stock.service;

import com.jeesite.common.lang.StringUtils;
import com.jeesite.modules.stock.dao.StockDailyDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class StockRsValuesService {

    @Autowired
    private StockDailyDao stockDailyDao;

    private Map<String, List<Map<String, Object>>> queryRsValues(List<String> stockCodes) {
        stockCodes.removeIf(item -> StringUtils.isBlank(item) || "null".equals(item));
        if (!stockCodes.isEmpty()) {
            List<Map<String, Object>> queryedList = new ArrayList<>();
            String startDate = "";
            for (String stockCode : stockCodes) {
                List<Map<String, Object>> rsArrDatas = stockDailyDao.findRsArrDatas(stockCode);
                if (rsArrDatas.isEmpty()) {
                    continue;
                }
                queryedList.addAll(rsArrDatas);
                startDate = rsArrDatas.getLast().get("date").toString();
                if (rsArrDatas.size() >= 60) {
                    break;
                }
            }
            if (!stockCodes.isEmpty()) {
                List<Map<String, Object>> allRsList = stockDailyDao.findRsArrDatasOfStockCodes(stockCodes, startDate);
                queryedList.addAll(allRsList);
            }
            return queryedList.stream().collect(Collectors.groupingBy(item -> String.valueOf(item.get("stock_code"))));
        }
        return new HashMap<>();
    }

    public void appendRsValues(List<Map<String, Object>> list) {
        if (list != null && !list.isEmpty()) {
            List<String> stockCodes = list.stream().map(item -> String.valueOf(item.get("stockCode"))).collect(Collectors.toList());

            Map<String, List<Map<String, Object>>> rsMap = queryRsValues(stockCodes);

            for (Map<String, Object> result : list) {
                String stockCode = String.valueOf(result.get("stockCode"));
                if (StringUtils.isBlank(stockCode) || "null".equalsIgnoreCase(stockCode)) {
                    continue;
                }
                List<Map<String, Object>> rsArrDatas = rsMap.get(stockCode);

                List<Double> stockWeighted8dRSArr = new ArrayList<>();
                List<Double> stockWeighted12dRSArr = new ArrayList<>();
                List<Double> stockWeighted24dRSArr = new ArrayList<>();
                List<Double> stockWeighted44dRSArr = new ArrayList<>();
                List<Double> stockWeighted64dRSArr = new ArrayList<>();
                List<Double> stockWeighted128dRSArr = new ArrayList<>();
                List<Double> stockWeighted252dRSArr = new ArrayList<>();

                List<Double> sectorWeighted8dRSArr = new ArrayList<>();
                List<Double> sectorWeighted12dRSArr = new ArrayList<>();
                List<Double> sectorWeighted24dRSArr = new ArrayList<>();
                List<Double> sectorWeighted44dRSArr = new ArrayList<>();
                List<Double> sectorWeighted64dRSArr = new ArrayList<>();
                List<Double> sectorWeighted128dRSArr = new ArrayList<>();
                List<Double> sectorWeighted252dRSArr = new ArrayList<>();

                result.put("DROpen", result.get("open"));
                result.put("DRClose", result.get("close"));
                result.put("DRHigh", result.get("high"));
                result.put("DRLow", result.get("low"));
                result.put("WROpen", result.get("WeekOpen"));
                result.put("WRHigh", result.get("Weekhigh"));
                result.put("WRLow", result.get("Weeklow"));
                result.put("WRClose", result.get("close"));

                if (rsArrDatas != null) {
                    rsArrDatas.sort((o1, o2) -> String.valueOf(o2.get("date")).compareTo(String.valueOf(o1.get("date"))));
                    for (int i = rsArrDatas.size() - 1; i >= 0; i--) {
                        Map<String, Object> rsArrData = rsArrDatas.get(i);

                        stockWeighted8dRSArr.add(Double.parseDouble(rsArrData.get("stock_weighted_8d_RS") == null ? "0"
                                : rsArrData.get("stock_weighted_8d_RS").toString()));
                        stockWeighted12dRSArr.add(Double.parseDouble(rsArrData.get("stock_weighted_12d_RS") == null ? "0"
                                : rsArrData.get("stock_weighted_12d_RS").toString()));
                        stockWeighted24dRSArr.add(Double.parseDouble(rsArrData.get("stock_weighted_24d_RS") == null ? "0"
                                : rsArrData.get("stock_weighted_24d_RS").toString()));
                        stockWeighted44dRSArr.add(Double.parseDouble(rsArrData.get("stock_weighted_44d_RS") == null ? "0"
                                : rsArrData.get("stock_weighted_44d_RS").toString()));
                        stockWeighted64dRSArr.add(Double.parseDouble(rsArrData.get("stock_weighted_64d_RS") == null ? "0"
                                : rsArrData.get("stock_weighted_64d_RS").toString()));
                        stockWeighted128dRSArr.add(Double.parseDouble(rsArrData.get("stock_weighted_128d_RS") == null ? "0"
                                : rsArrData.get("stock_weighted_128d_RS").toString()));
                        stockWeighted252dRSArr.add(Double.parseDouble(rsArrData.get("stock_weighted_252d_RS") == null ? "0"
                                : rsArrData.get("stock_weighted_252d_RS").toString()));

                        sectorWeighted8dRSArr.add(Double.parseDouble(rsArrData.get("sector_weighted_8d_RS") == null ? "0"
                                : rsArrData.get("sector_weighted_8d_RS").toString()));
                        sectorWeighted12dRSArr.add(Double.parseDouble(rsArrData.get("sector_weighted_12d_RS") == null ? "0"
                                : rsArrData.get("sector_weighted_12d_RS").toString()));
                        sectorWeighted24dRSArr.add(Double.parseDouble(rsArrData.get("sector_weighted_24d_RS") == null ? "0"
                                : rsArrData.get("sector_weighted_24d_RS").toString()));
                        sectorWeighted44dRSArr.add(Double.parseDouble(rsArrData.get("sector_weighted_44d_RS") == null ? "0"
                                : rsArrData.get("sector_weighted_44d_RS").toString()));
                        sectorWeighted64dRSArr.add(Double.parseDouble(rsArrData.get("sector_weighted_64d_RS") == null ? "0"
                                : rsArrData.get("sector_weighted_64d_RS").toString()));
                        sectorWeighted128dRSArr.add(Double.parseDouble(rsArrData.get("sector_weighted_128d_RS") == null ? "0"
                                : rsArrData.get("sector_weighted_128d_RS").toString()));
                        sectorWeighted252dRSArr.add(Double.parseDouble(rsArrData.get("sector_weighted_252d_RS") == null ? "0"
                                : rsArrData.get("sector_weighted_252d_RS").toString()));
                    }
                }
                result.put("stockWeighted8dRSArr", stockWeighted8dRSArr);
                result.put("stockWeighted12dRSArr", stockWeighted12dRSArr);
                result.put("stockWeighted24dRSArr", stockWeighted24dRSArr);
                result.put("stockWeighted44dRSArr", stockWeighted44dRSArr);
                result.put("stockWeighted64dRSArr", stockWeighted64dRSArr);
                result.put("stockWeighted128dRSArr", stockWeighted128dRSArr);
                result.put("stockWeighted252dRSArr", stockWeighted252dRSArr);

                result.put("sectorWeighted8dRSArr", sectorWeighted8dRSArr);
                result.put("sectorWeighted12dRSArr", sectorWeighted12dRSArr);
                result.put("sectorWeighted24dRSArr", sectorWeighted24dRSArr);
                result.put("sectorWeighted44dRSArr", sectorWeighted44dRSArr);
                result.put("sectorWeighted64dRSArr", sectorWeighted64dRSArr);
                result.put("sectorWeighted128dRSArr", sectorWeighted128dRSArr);
                result.put("sectorWeighted252dRSArr", sectorWeighted252dRSArr);
            }
        }
    }
}
